import Common from '../common'
var Js = Common('shopProduct')
export default {
  name: 'shopProduct',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: 'ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'product_no',
          label: '商品NO',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 100
        },
        {
          key: 'title',
          label: '商品名称',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入商品名称',
            clearable: true,
            defaultValue: '',
            optionsName: '',
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.title = ['LIKE', ['%' + val + '%']];
              } else {
                delete self.searchItem.title;
              };

            },
          },
        },
        {
          key: 'price',
          label: '商品价格',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'stock',
          label: '库存',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          width: 150,
        },
        {
          key: 'category_id',
          label: '商品类别',
          application: ['编辑', '添加'],
          formatter: function(val, tests) {
            return val.labelTit
          },
          componentName: 'sls-select',
          optionsName: 'labelOptions',
          listType: 'normal',
          limit: 10,
          defaultProps: {
            label: 'title',
            value: 'id',
          },
          placeholder: '请选择类别',
          width: 150,
          header_search: {
            componentName: 'sls-select',
            optionsName: 'labelOptions',
            placeholder: '请选择类别',
            multiple: true,
            defaultValue: [],
            changeFunc: function(val, self) {
              if (val && val.length > 0) {
                self.FIND_IN_SET = [];
                self.FIND_IN_SET.push({
                  type: 'or',
                  arr: val,
                  key: 'category_id'
                });
              } else {
                self.FIND_IN_SET = [];
              };
            },
          }
        },
        {
          key: "is_fprice",
          label: '商品状态',
          application: ['编辑', '添加'],
          componentName: 'sls-select',
          optionsName: 'fpriceOptions',
          listType: 'normal',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          formatter: function(val) {
            if (val.is_fprice == 1) {
              return '普通商品';
            } else {
              return '单点商品';
            };
          }
        },
        {
          key: 'listorder',
          label: '排序',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          width: 100
        },
        {
          key: 'mainImg',
          label: '展示图',
          customSlot: 'mainImg',
          application: ['编辑', '添加'],
          componentName: 'upload',
          listType: 'normal',
          limit: 10,
          dialogStyle: 'width:90%',
          width: 100
        },
        {
          key: 'bannerImg',
          label: '详情图',
          customSlot: 'bannerImg',
          application: ['编辑', '添加'],
          componentName: 'upload',
          listType: '',
          limit: 10,
          dialogStyle: 'width:90%',
        },
        {
          key: 'content',
          label: '内容',
          application: ['编辑', '添加'],
          componentName: 'tinymce-editor',
          listType: '',
          dialogStyle: 'width:90%',
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };
            },
          },
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 110
        },
      ],
      // 按钮配置
      btn_info: [{
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {
            apiName: function(self) {
              return "api_productUpdate"
            },
            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '编辑'
          },
          func: {
            apiName: function(self) {
              return "api_productUpdate"
            },
            formData: function(self) {
              return self.formData
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id,
                },
                data: self.submitData
              };
              return postData;
            }
          },
        },
        {
        	type: 'info',
        	icon: 'edit',
        	size: 'mini',
        	position: 'header',
        	text: function(data) {
        		return '添加'
        	},
        	funcType: 'func',
        	func: {
        		func: function(self) {
        			self.$router.push({
        				path: '/home/productdetail',
        				name: '平台商品详情',
        				params: {
        					type: 1,
        					delKeepAlive: true,
                  hasSku: true,
                  categorySearchItem: {
                    shop_no:self.shop_no
                  },
        					fixProduct: {shop_no:self.shop_no},
        					fixSku: {shop_no:self.shop_no},
        					backPath: '/home/shopProduct'
        				}
        			});
        		},
        	},
        },

        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          noAuth: true,
          text: function(data) {
            return '返回'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              var fullPath = self.$route.fullPath;
              var tabList = self.$store.state.tabs.tabs;
              var routerRes = self.$$getBackRoute(tabList, fullPath);
              self.$store.commit('REMOVE_TABS', {
                fullPath: fullPath,
              });
              if (routerRes.backRes) {
                self.$router.push(routerRes.backRes);
              } else {
                self.$router.push(routerRes.newRes);
              };
            },
          },
        },
      ],

      searchItem: {
        shop_no: ''
      },
      startSearchItem: {
        shop_no: ''
      },
      optionData: {
        labelOptions: [],
        skuOptions: [],
        fpriceOptions: [{
          text: '普通商品',
          value: 1
        }, {
          text: '单点商品',
          value: 0
        }],
        statusOptions: [{
          text: '启用',
          value: 1
        }, {
          text: '禁用',
          value: -1
        }],
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      },

    }

  },

  mounted() {
    const self = this;
    self.table_arguments.tree_props = {
      children: 'child',
      child: 'child',
      hasChildren: 'child'
    };

    self.shop_no = this.$route.params.shop_no;
    self.user_no = this.$route.params.user_no;
    self.backPath = this.$route.params.backPath;
    self.searchItem.shop_no = self.shop_no;
    self.startSearchItem.shop_no = self.shop_no;
    // if(this.$store.state.user.userinfo.role!=3){
    // 	self.searchItem.shop_no = this.$store.state.user.userinfo.user_no;
    // 	self.startSearchItem.shop_no = this.$store.state.user.userinfo.user_no;
    // }
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMainData();
      this.initLabelData();
    },

    async initLabelData(isNew) {

      const self = this;
      const postData = {};
      postData.token = self.$store.getters.getToken;
      postData.searchItem = {
        type: 4,
        shop_no: this.$route.params.shop_no
      };
      var res = await self.$$api_labelGet({
        data: postData
      });
      self.optionData.labelOptions = res.info.data;

    },


    /**
     * 列表主函数
     */
    async initMainData(isNew) {

      const self = this;
      self.table_arguments.loading = true;
      const postData = {};
      if (isNew) {
        self.paginate.currentPage = 1;
      };
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      if (self.FIND_IN_SET.length > 0) {
        postData.FIND_IN_SET = self.$$cloneForm(self.FIND_IN_SET);
      };
      postData.getAfter = {
        all_stock: {
          tableName: 'Sku',
          middleKey: 'id',
          key: 'product_id',
          condition: '=',
          info: ['id'],
          compute: {
            number: ['sum', 'stock', {
              status: 1
            }]
          }
        }
      }
      postData.order = {
        listorder: 'asc'
      }
      var res = await self.$$api_productGet({
        data: postData
      });
      for (var i = 0; i < res.info.data.length; i++) {
        res.info.data[i]['labelTit'] = [];
        var data = res.info.data[i]['label'];
        for (let key in data) {
          res.info.data[i]['labelTit'].push(data[key]['title']);
        }
        res.info.data[i]['labelTit'] = res.info.data[i]['labelTit'].join(',');
      }
      self.mainData = res.info.data;
      self.paginate.count = res.info.total;
      self.table_arguments.loading = false;

    }

  },


}
